package com.xbongbong.crm.perform.query.detail;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmDetailBaseInfoService;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.OwnerAndCoUserHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.explain.FormDataDetailFormExplainHandleName;
import com.xbongbong.parent.parse.data.FormDataGetAnalysisDataHelp;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.help.ShowFormatHelp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName
 * @Description 联系人表单详情页基本信息业务层
 * @Date 2022/5/17 19:15
 * @Version 1.0
 **/
@Service("contactDetailBaseInfoServiceImpl")
public class ContactDetailBaseInfoServiceImpl extends AbstractCrmDetailBaseInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ContactDetailBaseInfoServiceImpl.class);

    @Resource
    private OwnerAndCoUserHelp ownerAndCoUserHelp;
    @Resource
    private FormDataDetailFormExplainHandleName formDataDetailFormExplainHandleName;
    @Resource
    private ShowFormatHelp showFormatHelp;
    @Resource
    private FormDataGetAnalysisDataHelp formDataGetAnalysisDataHelp;

    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.CONTACT.getCode());
    }

    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer businessType) throws XbbException {

    }

    @Override
    public HandlerExplainVO handleExplain(List<FieldAttrEntity> explainList, PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, HandlerExplainDTO handlerExplainDTO) throws XbbException {
        HandlerExplainVO handlerExplainVO = super.handleExplain(explainList, formEntity, paasFormDataEntityExt, handlerExplainDTO);
        // 联系人特殊字段解释处理
        handleSpecialExplain(handlerExplainVO.getHeadList(), handlerExplainDTO.getPlatform());
        return handlerExplainVO;
    }

    @Override
    public JSONObject analysisData(PaasFormDataEntityExt formData, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO) throws XbbException {
        List<FieldAttrEntity> list = handlerExplainVO.getHeadList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(list.size());
        list.forEach(item->explainMap.put(item.getAttr(), item));
        JSONObject data = commonAnalysisAllData(formData, handlerExplainDTO, handlerExplainVO, list, explainMap);

        //联系人特殊数据格式化
        analysisSpecialData(data, handlerExplainDTO.getPlatform(), handlerExplainVO.getHeadList());
        return data;
    }

    /**
     * 联系人特殊数据解析
     * @param data
     * @param platform
     * @param explainList
     * @throws XbbException
     */
    private void analysisSpecialData(JSONObject data, String platform, List<FieldAttrEntity> explainList) throws XbbException {
        showFormatHelp.formatLinkBusiness4Show(data, ContactEnum.CUSTOMER_NAME.getAttr(), ContactEnum.CUSTOMER_NAME_LINK_TEXT.getAttr(), ContactEnum.CUSTOMER_NAME.getFieldType());

        // 移动端联系人头像不回显
        if (Objects.equals(PlatFormEnum.DINGTALK.getValue(), platform)) {
            data.remove(ContactEnum.AVATAR.getAttr());
        }
    }

    /**
     * 联系人特殊数据格式化
     * @param headList
     * @param platForm
     */
    private void handleSpecialExplain(List<FieldAttrEntity> headList, String platForm ) {
        // 移动端联系人头像不回传给前端，经产品高梦娜确认
        if (Objects.equals(PlatFormEnum.DINGTALK.getValue(), platForm)) {
            headList.removeIf(item -> Objects.equals(ContactEnum.AVATAR.getAttr(), item.getAttr()));
        }
    }
}
